﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ModelLayer;

namespace DBLayer
{
    public class DBPartBooking
    {
        private SqlConnection con;

        public DBPartBooking()
        {
            con = DBConnection.getInstance().getDBCon();
        }

        public int CreatePartBooking(Show show)
        {
            int pbID = 0;
            SqlParameter idParam = new SqlParameter("@Show_id", SqlDbType.Int);
            idParam.Value = show.Show_Id;

            SqlDataReader reader = null;

            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandText = "INSERT INTO PartBooking (Show_id)" +
                              "VALUES (@Show_id)" + 
                              "SELECT CAST(SCOPE_IDENTITY() AS int)";

            com.Parameters.Add(idParam);

            try
            {
                reader = com.ExecuteReader();

                if (reader.Read())
                    pbID = reader.GetInt32(0);

                reader.Close();

            }
            catch (SqlException e)
            {
                Console.WriteLine("Error: " + e);
            }

            return pbID;
        }

        public PartBooking GetPartBooking(int part_id, Show show)
        {
            PartBooking part = null;

            SqlDataReader reader = null;

            SqlParameter idParam = new SqlParameter("@part_id", SqlDbType.Int);
            idParam.Value = part_id;

            SqlCommand command = new SqlCommand();
            command.Connection = con;
            command.CommandText = "SELECT id, isPaid, bookingId FROM PartBooking " +
                                    "WHERE id = @part_id ";

            command.Parameters.Add(idParam);

            try
            {
                reader = command.ExecuteReader();


                while (reader.Read())
                {
                    try
                    {
                        part = new PartBooking(reader.GetInt32(0), show, reader.GetBoolean(1), reader.GetInt32(2));
                    }
                    catch (System.InvalidCastException ex)
                    {
                        Console.WriteLine(ex);
                    }
                }
                reader.Close();
            }
            catch (SqlException e)
            {
                Console.WriteLine(e);
            }
            return part;
        }

        public bool InsertReservedSeats(List<Seat> seats, PartBooking pb)
        {
            bool inserted = false;
            int partId = pb.Id;

            foreach (Seat s in seats)
            {
                SqlParameter partIdParam = new SqlParameter("@partId", SqlDbType.Int);
                partIdParam.Value = partId;

                SqlParameter seatIdParam = new SqlParameter("@seatId", SqlDbType.Int);
                seatIdParam.Value = s.SeatId;

                SqlCommand com = new SqlCommand();
                com.Connection = con;
                com.CommandText = "INSERT INTO Part_Seat (part_id, seat_id) " +
                                  "VALUES (@partId, @seatId) ";
                com.Parameters.Add(partIdParam);
                com.Parameters.Add(seatIdParam);

                try
                {
                    int result = com.ExecuteNonQuery();
                    if (!result.Equals(0))
                    {
                        inserted = true;
                    }

                }
                catch (SqlException e)
                {
                    Console.WriteLine("Error: " + e);
                }
            }

            return inserted;
        }

        public List<PartBooking> GetPartBookingByBookingId(Booking booking)
        {
            List<PartBooking> pbList = new List<PartBooking>();

            SqlDataReader reader = null;

            SqlParameter idParam = new SqlParameter("@booking_id", SqlDbType.Int);
            idParam.Value = booking.BookingId;

            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandText = "SELECT  FROM PartBooking";
            com.Parameters.Add(idParam);

            try
            {
                reader = com.ExecuteReader();

                while (reader.Read())
                {
                    //pbList.Add(new PartBooking())
                }
            }
            catch (SqlException e)
            {
                Console.WriteLine("Error: " + e);
            }

            return pbList;
        }
    }
}
